package ch10;

import java.util.List;

import org.omg.CORBA.DATA_CONVERSION;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

public class ListClass {
	
	public static void main(String[] args) {
		//List集合
		//用于存放有序的数据，list集合的元素可能重复
		List list = new ArrayList();
		

		Computer computer = new Computer("HP",5000);
		Computer computer2 = new Computer("HW",5500);
		Computer computer3 = new Computer("HW2",5500);

		//新增操作
		list.add(computer);
		list.add(computer2);
		list.add(computer3);
		//list.add(1,computer3);//数据插入
		list.add(computer2);//list可以把同一个对象保存到集合中，可以保存多次，即该对象可以重复
		System.out.println(list.size());
		
		//查询-遍历查询
		Iterator iterator = list.iterator();
		
		while (iterator.hasNext()) {
			Computer computer4 = (Computer)iterator.next();
			System.out.println(computer4);	
		}
		
		System.out.println("=================================");
		
		//使用for循环迭代集合
		for(int i=0;i<list.size();i++) {
			Computer computer4 = (Computer)list.get(i);
			System.out.println(computer4);
		}
		
		//可以删除对象或根据索引进行删除
		list.remove(0);
		list.remove(computer2);
		System.out.println("===========================================");
		
		//集合如果不指定数据类型，则可以保存任何引用数据类型
		list.add(new String("你好，集合"));
		list.add(new Date());
		//for each
		for(Object object : list) {
			System.out.println(object.toString());
			//在取数据时，必须先进行类型的判断
			if(object instanceof String) {
				String str = (String)object;
			}else if(object instanceof Computer){
				Computer comp = (Computer)object;
			}else if (object instanceof Date) {
				Date date = (Date)object;
			}
		}
		
	}

}
